package view
{
    import flash.display.*;

    public class Enemy extends Sprite
    {
        public static const TYPE_NORMAL:int = 1;
        public static const TYPE_YELLOW:int = 2;
        public static const TYPE_RED:int = 3;
        public static const TYPE_PINK:int = 4;
        public static const TYPE_ORANGE:int = 5;

        public static const FIRE_NORMAL:int = 1;

        public static const MOVE_NORMAL:int = 1;
        public static const MOVE_SIN:int = 3;
        public static const MOVE_COS:int = 7;
        public static const MOVE_SIN_FAST:int = 15;
        public static const MOVE_COS_FAST:int = 31;

        public var type:int;
        public var speed:Number;
        public var healthPoint:int;
        public var attackPoint:int;
        public var fireMode:int;
        public var moveMode:int;

        private var _hit:Boolean;

        public function Enemy(t:int=0)
        {
            type = t;
            _hit = false;
            switch(type)
            {
                case TYPE_NORMAL:
                    speed = 0.05;
                    healthPoint = 20;
                    attackPoint = 1;
                    fireMode = FIRE_NORMAL;
                    moveMode = MOVE_NORMAL;
                    break;
                case TYPE_YELLOW:
                    speed = 0.025;
                    healthPoint = 40;
                    attackPoint = 2;
                    fireMode = FIRE_NORMAL;
                    moveMode = MOVE_NORMAL | MOVE_SIN;
                    break;
                case TYPE_RED:
                    speed = 0.025;
                    healthPoint = 40;
                    attackPoint = 2;
                    fireMode = FIRE_NORMAL;
                    moveMode = MOVE_NORMAL | MOVE_COS;
                    break;
                case TYPE_PINK:
                    speed = 0.05;
                    healthPoint = 60;
                    attackPoint = 3;
                    fireMode = FIRE_NORMAL;
                    moveMode = MOVE_NORMAL | MOVE_SIN_FAST;
                    break;
                case TYPE_ORANGE:
                    speed = 0.05;
                    healthPoint = 60;
                    attackPoint = 3;
                    fireMode = FIRE_NORMAL;
                    moveMode = MOVE_NORMAL | MOVE_COS_FAST;
                    break;
                default:
                    break;
            }
        }

        public function get hit():Boolean
        {
            return _hit;
        }

        public function set hit(v:Boolean):void
        {
            if(_hit != v)
            {
                _hit = v;
                if(_hit)
                {
                    displayColor(0xFFFFFF);
                }
                else
                {
                    display();
                }
            }
        }

        public function displayColor(c:uint):void
        {
            graphics.clear();
            graphics.beginFill(c);
            graphics.moveTo(0, 0);
            graphics.lineTo(24, -16);
            graphics.lineTo(-24, -16);
            graphics.endFill();
            
            graphics.beginFill(c);
            graphics.moveTo(0, -16);
            graphics.lineTo(8, -24);
            graphics.lineTo(-8, -24);
            graphics.endFill();
            
            graphics.beginFill(0x606060);
            graphics.moveTo(0, 24);
            graphics.lineTo(4, -10);
            graphics.lineTo(0, -28);
            graphics.lineTo(-4, -10);
            graphics.endFill();
        }

        public function displayBoss(c:uint):void
        {
            graphics.clear();
            graphics.beginFill(c);
            graphics.moveTo(0, 0);
            graphics.lineTo(48, -32);
            graphics.lineTo(-48, -32);
            graphics.endFill();
            
            graphics.beginFill(c);
            graphics.moveTo(0, -32);
            graphics.lineTo(16, -48);
            graphics.lineTo(-16, -48);
            graphics.endFill();
            
            graphics.beginFill(0x606060);
            graphics.moveTo(0, 48);
            graphics.lineTo(8, -20);
            graphics.lineTo(0, -56);
            graphics.lineTo(-8, -20);
            graphics.endFill();
        }

        public function display():void
        {
            switch(type)
            {
                case TYPE_NORMAL:
                    displayColor(0x00C000);
                    break;
                case TYPE_YELLOW:
                    displayColor(0xFFFF00);
                    break;
                case TYPE_RED:
                    displayColor(0xFF0000);
                    break;
                case TYPE_PINK:
                    displayColor(0xFFA0FF);
                    break;
                case TYPE_ORANGE:
                    displayColor(0xFFAF00);
                    break;
                default:
                    break;
            }
        }

        public function update(t:Number):void
        {
            if((moveMode & MOVE_NORMAL) == MOVE_NORMAL)
            {
                y += t * speed;
            }
            if((moveMode & MOVE_SIN) == MOVE_SIN)
            {
                x = 180 * Math.sin(Math.PI * y / 180);
            }
            if((moveMode & MOVE_COS) == MOVE_COS)
            {
                x = 180 * Math.cos(Math.PI / 2 + Math.PI * y / 180);
            }
            if((moveMode & MOVE_SIN_FAST) == MOVE_SIN_FAST)
            {
                x = 100 * Math.sin(Math.PI * y / 90);
            }
            if((moveMode & MOVE_SIN_FAST) == MOVE_SIN_FAST)
            {
                x = 100 * Math.sin(Math.PI / 2 + Math.PI * y / 90);
            }
        }
    }
}
